﻿using System;
using System.Linq;

namespace SERVICES.Services
{
    public static class User
    {
        public static bool Authenticate(string username, string password)
        {
            using (var context = new WMILKEntities2())
            {
                var accounts =
                    context.USERS.Where(
                        a => a.US_NAME == username
                            && a.US_PASSWORD == password).ToList();

                if (accounts.Count() == 1)
                {
                    return true;
                }
            }
            return false;
        }

        public static USER GetUser(int userID)
        {
            try
            {
                using (var db = new WMILKEntities2())
                {
                    var user = db.USERS.Where(u => u.US_ID == userID).Select(u => u).FirstOrDefault();
                    return user;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Không tìm thấy User"+ex.Message);
            }

        }

        public static bool Register(string registerUserName, string registerFullName, string registerPassword)
        {
            using (var db = new WMILKEntities2())
            {
                var user = db.USERS.Where(x => x.US_NAME == registerUserName).FirstOrDefault();
                if (user == null)
                {
                    var newUser = new USER();
                    newUser.US_NAME = registerUserName;
                    newUser.US_USERNAME = registerFullName;
                    newUser.US_PASSWORD = registerPassword;
                    newUser.US_ROLE = 0;
                    db.USERS.Add(newUser);
                    db.SaveChanges();
                    return true;
                }
            }
            return false;
        }
    }
}
